package org.csy.work.dao.impl;

import org.csy.work.dao.ProductDao;
import org.csy.work.entity.Product;
import org.csy.work.util.DbUtil;

import javax.xml.transform.Result;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author CSY
 * @date 2024-11-28
 */
public class ProductDaoImpl implements ProductDao {
    @Override
    public List<Product> getProductAll() {
        List<Product> list = new ArrayList<Product>();
        String sql = "select product_id,product_name,product_price,product_image" +
                ",product_count,product_desc,product_sale,product_date,product_type,product_status" +
                " from product";
        Connection con = DbUtil.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                Product product = new Product();
                product.setProductId(rs.getString("product_id"));
                product.setProductName(rs.getString("product_name"));
                product.setProductPrice(rs.getDouble("product_price"));
                product.setProductImage(rs.getString("product_image"));
                product.setProductCount(rs.getInt("product_count"));
                product.setProductDesc(rs.getString("product_desc"));
                product.setProductSale(rs.getInt("product_sale"));
                product.setProductStatus(rs.getInt("product_status"));
                product.setProductDate(rs.getDate("product_date"));
                product.setProductType(rs.getString("product_type"));
                list.add(product);
            }
            rs.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DbUtil.close(rs, ps, con);
        }
        return list;
    }

    @Override
    public Product getProductById(String id) {
        Product product = null;
        String sql = "select product_id,product_name,product_price,product_image" +
                ",product_count,product_desc,product_sale,product_date,product_type,product_status" +
                " from product where product_id=?";
        Connection con = DbUtil.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                product = new Product();
                product.setProductId(rs.getString("product_id"));
                product.setProductName(rs.getString("product_name"));
                product.setProductPrice(rs.getDouble("product_price"));
                product.setProductImage(rs.getString("product_image"));
                product.setProductCount(rs.getInt("product_count"));
                product.setProductDesc(rs.getString("product_desc"));
                product.setProductSale(rs.getInt("product_sale"));
                product.setProductStatus(rs.getInt("product_status"));
                product.setProductDate(rs.getDate("product_date"));
                product.setProductType(rs.getString("product_type"));
            }
            rs.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DbUtil.close(rs, ps, con);
        }
        return product;
    }
    @Override
    public int insert(Product product) {
        int n = 0;
        String sql ="insert into product(product_id,product_name,product_type,product_price,product_count,product_image,product_date,product_desc,product_sale,product_status) " +
                "values (?,?,?,?,?,?,?,?,?,?) ;";
        Connection con = DbUtil.getConnection();
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, product.getProductId());
            ps.setString(2, product.getProductName());
            ps.setString(3, product.getProductType());
            ps.setDouble(4, product.getProductPrice());
            ps.setInt(5, product.getProductCount());
            ps.setString(6, product.getProductImage());
            ps.setObject(7,product.getProductDate());
            ps.setString(8,product.getProductDesc());
            ps.setInt(9,product.getProductSale());
            ps.setInt(10,product.getProductStatus());
            n = ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DbUtil.close(null,ps,con);
        }
        return n;
    }

    @Override
    public int update(Product product) {
        int n =0;
        String sql = "update product set product_name=?,product_type=?,product_price=?,product_count=?," +
                "product_image=?,product_date=?,product_desc=?,product_sale=?,product_status=? " +
                "where product_id=?";
        Connection con = DbUtil.getConnection();
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, product.getProductName());
            ps.setString(2, product.getProductType());
            ps.setDouble(3, product.getProductPrice());
            ps.setInt(4, product.getProductCount());
            ps.setString(5, product.getProductImage());
            ps.setObject(6,product.getProductDate());
            ps.setString(7,product.getProductDesc());
            ps.setInt(8,product.getProductSale());
            ps.setInt(9,product.getProductStatus());
            ps.setString(10, product.getProductId());
            n = ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DbUtil.close(null,ps,con);
        }
        return n;
    }

    @Override
    public int delete(String productId) {
        int n = 0;
        String sql = "delete from product where product_id = ?";
        Connection con = DbUtil.getConnection();
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, productId);
            n = ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DbUtil.close(null,ps,con);
        }
        return n;
    }
}
